package training.minggu5.maven.service.impl;

import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import training.minggu5.maven.domain.Barang;
import training.minggu5.maven.service.BarangService;

@Service("barangService")
@Transactional(readOnly=true)
public class BarangServiceImpl implements BarangService {

    @Autowired
    private SessionFactory sessionFactory;
    private String hqlFindAll = "from Barang";
    private String hqlFindByKode = "from Barang b where b.kodeBarang = :kode";

    @Override
    @Transactional(readOnly=false)
    public void saveBarang(Barang barang) {
        sessionFactory.getCurrentSession().saveOrUpdate(barang);
    }

    @Override
    @Transactional(readOnly=false)
    public void deleteBarang(Barang barang) {
        sessionFactory.getCurrentSession().delete(barang);
    }

    @Override
    public Barang findBarangByKode(String kode) {
        return (Barang) sessionFactory.getCurrentSession().createQuery(hqlFindByKode)
                .setParameter("kode", kode).uniqueResult();
    }

    @Override
    public List<Barang> findAllBarang() {
        return sessionFactory.getCurrentSession().createQuery(hqlFindAll).list();
    }
    
}
